﻿// Page1.cpp: 实现文件
//

#include "pch.h"
#include "DCM.h"
#include "Page1.h"
#include "CDCMEngine.h"
#include "DCMDlg.h"
#include <locale.h>


// Page1 对话框
Page1* g_pPage1 = NULL;
extern CDCMEngine* g_pEngine;
extern CDCMDlg* g_pDCMDlg;
extern Page2* g_pPage2;


IMPLEMENT_DYNAMIC(Page1, CDialogEx)


Page1::Page1(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_Page1, pParent)
	, m_radio_isSwtich(0)
	, m_radio_playMode(0)
	, m_radio_isRanking(0)
	, m_radio_isWallet(0)
	, m_radio_isWithdrawal(0)
{

}

Page1::~Page1()
{
}

void Page1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);





	DDX_Control(pDX, IDC_EDIT_accessPath, m_edit_accesPath);
	DDX_Control(pDX, IDC_EDIT_Simulator_Path, m_edit_simulator_path);
	DDX_Control(pDX, IDC_EDIT_wndSum, m_edit_wndSum);
	DDX_Radio(pDX, IDC_RADIO14, m_radio_isSwtich);

	DDX_Radio(pDX, IDC_RADIO19, m_radio_playMode);
	DDX_Radio(pDX, IDC_RADIO21, m_radio_isRanking);
	DDX_Control(pDX, IDC_EDIT_ranking, m_edit_ranking);
	DDX_Control(pDX, IDC_EDIT_ranking2, m_edit_ranking2);
	DDX_Control(pDX, IDC_EDIT_ranking3, m_edit_ranking3);
	DDX_Radio(pDX, IDC_RADIO23, m_radio_isWallet);
	DDX_Radio(pDX, IDC_RADIO25, m_radio_isWithdrawal);
	DDX_Control(pDX, IDC_EDIT_walletPwd, m_edit_walletPwd);
}


BEGIN_MESSAGE_MAP(Page1, CDialogEx)
	ON_BN_CLICKED(IDC_RADIO14, &Page1::OnBnClickedRadio14)
	ON_BN_CLICKED(IDC_RADIO15, &Page1::OnBnClickedRadio15)
	ON_BN_CLICKED(IDC_RADIO19, &Page1::OnBnClickedRadio19)
	ON_BN_CLICKED(IDC_RADIO20, &Page1::OnBnClickedRadio20)
	ON_BN_CLICKED(IDC_BTN_RESUME, &Page1::OnBnClickedBtnResume)
	ON_BN_CLICKED(IDC_BTN_CLOSE_ALL, &Page1::OnBnClickedBtnCloseAll)
	ON_BN_CLICKED(IDC_BTN_SUSPEND, &Page1::OnBnClickedBtnSuspend)
	ON_BN_CLICKED(IDC_BTN_SAVE, &Page1::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BTN_HANDLE_BIND, &Page1::OnBnClickedBtnHandleBind)
	ON_BN_CLICKED(IDC_BTN_SORT, &Page1::OnBnClickedBtnSort)
	ON_BN_CLICKED(IDC_BUTTON_getAccessFile, &Page1::OnBnClickedButtongetaccessfile)

	ON_BN_CLICKED(IDC_RADIO21, &Page1::OnBnClickedRadio21)
	ON_BN_CLICKED(IDC_RADIO22, &Page1::OnBnClickedRadio22)
	ON_BN_CLICKED(IDC_RADIO23, &Page1::OnBnClickedRadio23)
	ON_BN_CLICKED(IDC_RADIO24, &Page1::OnBnClickedRadio24)
	ON_BN_CLICKED(IDC_RADIO25, &Page1::OnBnClickedRadio25)
	ON_BN_CLICKED(IDC_RADIO26, &Page1::OnBnClickedRadio26)
END_MESSAGE_MAP()


// Page1 消息处理程序

BOOL Page1::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	g_pPage1 = this;
	

	//初始化加载配置
	m_edit_simulator_path.SetWindowText(g_pEngine->m_WndIni.strLDPath);

	m_edit_accesPath.SetWindowText(g_pEngine->m_WndIni.账号路径);

	m_edit_walletPwd.SetWindowText(g_pEngine->m_WndIni.钱包密码);

	if (g_pEngine->m_WndIni.账号路径.GetLength() > 0) {
		//读取
		getAccessFile();

	}


	CString 排位排名 = g_pEngine->m_WndIni.排位排名;
	CStringArray arr;
	Split(排位排名, arr, _T("-"));

	if (arr.GetCount() >= 3) {
		m_edit_ranking.SetWindowText(arr[0]);
		m_edit_ranking2.SetWindowText(arr[1]);
		m_edit_ranking3.SetWindowText(arr[2]);
	}
	m_edit_wndSum.SetWindowText(I2S(g_pEngine->m_WndIni.模拟器数量));


	if (g_pEngine->m_WndIni.开启切号 == 0) {

		m_radio_isSwtich = 0;

	}
	if (g_pEngine->m_WndIni.开启切号 == 1) {

		m_radio_isSwtich = 1;
		((CButton*)GetDlgItem(IDC_RADIO14))->SetCheck(FALSE);//选上
		((CButton*)GetDlgItem(IDC_RADIO15))->SetCheck(TRUE);//选上
	}

	if (g_pEngine->m_WndIni.对战模式 == 0) {

		m_radio_playMode = 0;

	}
	if (g_pEngine->m_WndIni.对战模式 == 1) {

		m_radio_playMode = 1;
		((CButton*)GetDlgItem(IDC_RADIO19))->SetCheck(FALSE);//选上
		((CButton*)GetDlgItem(IDC_RADIO20))->SetCheck(TRUE);//选上
	}


	if (g_pEngine->m_WndIni.开启排位 == 0) {

		m_radio_isRanking = 0;

	}

	if (g_pEngine->m_WndIni.开启排位 == 1) {

		m_radio_isRanking = 1;
		((CButton*)GetDlgItem(IDC_RADIO21))->SetCheck(FALSE);//选上
		((CButton*)GetDlgItem(IDC_RADIO22))->SetCheck(TRUE);//选上

	}

	if (g_pEngine->m_WndIni.开启注册钱包 == 0) {

		m_radio_isWallet = 0;

	}

	if (g_pEngine->m_WndIni.开启注册钱包 == 1) {

		m_radio_isWallet = 1;
		((CButton*)GetDlgItem(IDC_RADIO23))->SetCheck(FALSE);//选上
		((CButton*)GetDlgItem(IDC_RADIO24))->SetCheck(TRUE);//选上

	}


	if (g_pEngine->m_WndIni.开启提币 == 0) {

		m_radio_isWithdrawal = 0;

	}

	if (g_pEngine->m_WndIni.开启提币 == 1) {

		m_radio_isWithdrawal = 1;
		((CButton*)GetDlgItem(IDC_RADIO25))->SetCheck(FALSE);//选上
		((CButton*)GetDlgItem(IDC_RADIO26))->SetCheck(TRUE);//选上

	}

	return TRUE;  
}










//获取账号文件
void Page1::getAccessFile() {

	if (!accessList.IsEmpty()) {
		accessList.RemoveAll();
	}
	if (g_pDCMDlg) {
		if (g_pDCMDlg->m_lstWnd) {
			g_pDCMDlg->m_lstWnd.DeleteAllItems();
		}
	}

	CStdioFile file;
	CString FileName;
	m_edit_accesPath.GetWindowText(FileName);

	char* old_locale = _strdup(setlocale(LC_CTYPE, NULL));
	setlocale(LC_CTYPE, "chs");

	if (PathFileExists(FileName))
	{
		CFileException fileException;

		if (file.Open(FileName, CFile::typeText | CFile::modeReadWrite), &fileException) {

			CString str;
			int i = 1;

			while (file.ReadString(str)){
				if (str.GetLength() == 0) return;
				accessList.Add(str);
				CStringArray arr;
				Split(str, arr, _T("|"));
				if (arr.GetCount() >= 3) {

					//添加账号
					g_pDCMDlg->m_lstWnd.InsertItem(i - 1, I2S(i));
					g_pDCMDlg->m_lstWnd.SetItemText(i - 1, 2, arr[0]);
					i++;

				}
				else {
					MessageBox(_T("账号格式有误"), NULL, MB_ICONERROR);
					exit(0);
				}
			}


			//重置状态
			file.Flush();
			file.Close();

		}

	}
	setlocale(LC_CTYPE, old_locale); //还原语言区域的设置 
	free(old_locale);//还原区域设定操作
}



//保存配置
void Page1::OnBnClickedBtnSave()
{
	CString workPath = g_pEngine->m_strWorkPath;

	CString 雷电路径;

	m_edit_simulator_path.GetWindowText(雷电路径);

	if (雷电路径.GetLength() <= 0) {
		//路径为空
		MessageBox(_T("雷电模拟器路径为空"), NULL, MB_ICONERROR);
		return;
	}


	CString 账号路径;
	m_edit_accesPath.GetWindowText(账号路径);
	if (账号路径.GetLength() <= 0) {
		//路径为空
		MessageBox(_T("账号路径为空"), NULL, MB_ICONERROR);
		return;
	}
	WritePrivateProfileString(_T("窗口"), _T("雷电路径"), 雷电路径, workPath + _T("/全局配置.ini"));
	WritePrivateProfileString(_T("窗口"), _T("账号路径"), 账号路径, workPath + _T("/全局配置.ini"));



	CString 模拟器数量;
	m_edit_wndSum.GetWindowText(模拟器数量);
	WritePrivateProfileString(_T("窗口"), _T("模拟器数量"), 模拟器数量, workPath + _T("/全局配置.ini"));

	CString 钱包密码;
	m_edit_walletPwd.GetWindowText(钱包密码);
	WritePrivateProfileString(_T("窗口"), _T("钱包密码"), 钱包密码, workPath + _T("/全局配置.ini"));

	CString 排位排名;
	CString 排位排名1;
	m_edit_ranking.GetWindowText(排位排名1);
	CString 排位排名2;
	m_edit_ranking2.GetWindowText(排位排名2);
	CString 排位排名3;
	m_edit_ranking3.GetWindowText(排位排名3);

	排位排名.Format(_T("%s-%s-%s"), 排位排名1, 排位排名2, 排位排名3);

	WritePrivateProfileString(_T("窗口"), _T("排位排名"), 排位排名, workPath + _T("/全局配置.ini"));


	WritePrivateProfileString(_T("窗口"), _T("开启切号"), I2S(m_radio_isSwtich), workPath + _T("/全局配置.ini"));
	WritePrivateProfileString(_T("窗口"), _T("对战模式"), I2S(m_radio_playMode), workPath + _T("/全局配置.ini"));
	WritePrivateProfileString(_T("窗口"), _T("开启排位"), I2S(m_radio_isRanking), workPath + _T("/全局配置.ini"));
	WritePrivateProfileString(_T("窗口"), _T("开启注册钱包"), I2S(m_radio_isWallet), workPath + _T("/全局配置.ini"));
	WritePrivateProfileString(_T("窗口"), _T("开启提币"), I2S(m_radio_isWithdrawal), workPath + _T("/全局配置.ini"));

	if (g_pEngine) {

		g_pEngine->ReadWndIni();

		//g_pEngine->ReadWndIniById(g_pDCMDlg->m_checkIndex-1);

	}


	MessageBox(_T("保存成功!!!"), NULL, MB_OK);

}


//开始游戏
void Page1::OnBnClickedBtnHandleBind()
{
	g_pDCMDlg->Start();
}


//暂停游戏
void Page1::OnBnClickedBtnSuspend()
{
	g_pDCMDlg->Suspend();
}



//停止游戏
void Page1::OnBnClickedBtnCloseAll()
{
	g_pDCMDlg->Stop();
}


//恢复游戏
void Page1::OnBnClickedBtnResume()
{
	g_pDCMDlg->Resume();
}


//一件排列
void Page1::OnBnClickedBtnSort()
{
	g_pEngine->SortWnd();
}



//获取账号文件路径
void Page1::OnBnClickedButtongetaccessfile()
{
	CFileDialog dlg(TRUE, L"txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"(*.txt)|*.txt||");
	if (dlg.DoModal() == IDOK)
	{
		m_edit_accesPath.SetWindowText(dlg.GetPathName());
		UpdateData(FALSE);
		getAccessFile();
	}
}



void Page1::OnBnClickedRadio21()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio22()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio23()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio24()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio25()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio26()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio14()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}


void Page1::OnBnClickedRadio15()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}

void Page1::OnBnClickedRadio19()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}

void Page1::OnBnClickedRadio20()
{
	UpdateData(TRUE);

	UpdateData(FALSE);
}
